var app = getApp();
var token = wx.getStorageSync('kkk')
const APP_ID = 'wxee2be56031376d83';//输入小程序appid  
const APP_SECRET = 'a28339c62653ab0c842475f48de78175';//输入小程序app_secret  
var OPEN_ID = ''//储存获取到openid  
var SESSION_KEY = ''//储存获取到session_key  
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // if (getApp().data.server==""){
    //   console.log(getApp().data.server)
    //   getApp().data.server="我有数据了"
      
    // }
    wx.showNavigationBarLoading()
    var that = this;
    wx.login({
      success: function (res) {
        wx.request({
          //获取openid接口  
          url: 'https://api.weixin.qq.com/sns/jscode2session',
          data: {
            appid: APP_ID,
            secret: APP_SECRET,
            js_code: res.code,
            grant_type: 'authorization_code'
          },
          method: 'GET',
          success: function (res) {
            console.log(res.data)
            OPEN_ID = res.data.openid;//获取到的openid  
            SESSION_KEY = res.data.session_key;//获取到session_key  
            console.log(OPEN_ID.length)
            console.log(SESSION_KEY.length)
            that.setData({
              openid: res.data.openid.substr(0, 10) + '********' + res.data.openid.substr(res.data.openid.length - 8, res.data.openid.length),
              session_key: res.data.session_key.substr(0, 8) + '********' + res.data.session_key.substr(res.data.session_key.length - 6, res.data.session_key.length)
            })
          }
        })
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  send:function(){
    wx.showModal({
      title: '提示',
      content: '请输入正确手机号！',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else {
          console.log('用户点击取消')
        }

      }
    })
  },
  userphone:function(e){
    console.log(e.detail.value)
  },
  us:function(){
    wx.navigateTo({
      url: '../users/users',
    })
  },
  getOpenIdTap: function () {
    var that = this;
    wx.login({
      success: function (res) {
        wx.request({
          //获取openid接口  
          url: 'https://api.weixin.qq.com/sns/jscode2session',
          data: {
            appid: APP_ID,
            secret: APP_SECRET,
            js_code: res.code,
            grant_type: 'authorization_code'
          },
          method: 'GET',
          success: function (res) {
            console.log(res.data)
            OPEN_ID = res.data.openid;//获取到的openid  
            SESSION_KEY = res.data.session_key;//获取到session_key  
            console.log(OPEN_ID.length)
            console.log(SESSION_KEY.length)
            that.setData({
              openid: res.data.openid.substr(0, 10) + '********' + res.data.openid.substr(res.data.openid.length - 8, res.data.openid.length),
              session_key: res.data.session_key.substr(0, 8) + '********' + res.data.session_key.substr(res.data.session_key.length - 6, res.data.session_key.length)
            })
          }
        })
      }
    })
  }, 
  getPhoneNumber: function (e) {

    wx.login({

      success: function (res) {
        if (res.code) {
          //发起网络请求  
          console.log('获取到' + res.code)
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }


    })
  } ,
  
  getSystemInfo: function () {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        console.log(res)
        that.setData({
          systemInfo: res
        })
        that.update()
      }
    })
  },
  testSubmit: function (e) {
    var self = this;
    let _access_token ='10_ajg3mwO-L8mChZ8y24eVSL9JrI0qWuUcYxPXkZIhXABb2T96ANodUnQQBwBwhykOcvRTl3P29So5yaltpjYaONn8xNOlIwMIlv4kBJi9t_OkdCQbkCo10pphZfG4_iPa5kaylC6V7p8qWKLYPMKhAIAURP';
    let url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' + _access_token; 
    let _jsonData = {
        access_token: _access_token,
        touser: OPEN_ID,
        template_id: 'RfZ_rwUve73ZGbEfCrwWTvDmGhUPGMnzdgI56dCvsS0',
        form_id: e.detail.formId,
        page: "pages/my/my",
        data: {
          "keyword1": { "value": "测试数据一", "color": "#ff0000" },
          "keyword2": { "value": "测试数据二", "color": "#969696" },
          "keyword3": { "value": "测试数据三", "color": "#f2f2f2" },
          "keyword4": { "value": "测试数据四", "color": "#173177" },
        }
      }
    wx.request({
      url: url,
      data: _jsonData,
      method: 'post',
      success: function (res) {
        console.log(res)
      },
      fail: function (err) {
        console.log('request fail ', err);
      },
      complete: function (res) {
        console.log("request completed!");
      }

    })
  },
})